package com.xjm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AppConfig appConfig;

    //⾃定义的拦截器对象
    @Autowired
    private MyInterceptor myInterceptor;
    //拦截的名单
    private static final String[] REGISTER_URI = {
            "/admin/**",
            "/user/cartItem/**",
            "/user/address/**",
            "/user/order/**",
            "/user/update",
            "/user/info",
    };
    //不拦截的名单
    private static final String[] IGNORE_URI = {
            "/admin/login",
            "/admin/getCode",
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        注册⾃定义拦截器对象  todo
        registry.addInterceptor(myInterceptor) //添加拦截器
                .addPathPatterns(REGISTER_URI) //添加拦截名单
                .excludePathPatterns(IGNORE_URI); //添加不拦截的名单
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(appConfig.getUrlMapping() + "**").addResourceLocations("file:" + appConfig.getSavePath());
    }
}
